package com.yhty.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.yhty.dao.HouseDao;
import com.yhty.model.House;

/**
 *房屋dao实现类
 * 
 * @author roy 2011-2-22
 */
@Repository("houseDao")
public class HouseDaoImpl implements HouseDao {

	private JdbcTemplate jdbcTemplate;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yhty.dao.HouseDao#add(com.yhty.model.House)
	 */
	public void add(House house) {
		String sql = "insert into house(file_code,hos_type,address,hos_floor,"
				+ "location,bump_code,room_code,use_area,build_area,common_area) values"
				+ " (?,?,?,?,?,?,?,?,?,?)";
		getJdbcTemplate().update(
				sql,
				new Object[] { house.getFile_code(), house.getHos_type(),
						house.getAddress(), house.getHos_floor(),
						house.getLocation(), house.getBump_code(),
						house.getRoom_code(), house.getUse_area(),
						house.getBuild_area(), house.getCommon_area() });
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	@Resource(name = "jdbcTemplate")
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	private class HouseMapper implements RowMapper<House> {
		public House mapRow(ResultSet rs, int rowNum) throws SQLException {
			House h = new House();
			h.setFile_code(rs.getString("file_code"));
			h.setAddress(rs.getString("address"));
			h.setBuild_area(rs.getFloat("build_area"));
			h.setBump_code(rs.getString("bump_code"));
			h.setCommon_area(rs.getFloat("common_area"));
			h.setHos_floor(rs.getString("hos_floor"));
			h.setHos_type(rs.getString("hos_type"));
			h.setLocation(rs.getString("location"));
			h.setRoom_code(rs.getString("room_code"));
			h.setUse_area(rs.getFloat("use_area"));
			return h;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yhty.dao.HouseDao#getAll()
	 */
	public List<House> getAll() {
		String sql = "select * from house";
		return getJdbcTemplate().query(sql, new HouseMapper());
	}

	/*
	 * 采用2.5开始的类属性解析器BeanPropertyRowMapper对数据
	 * 进行自动映射。
	 * 注意： 1、被映射的类本身要为顶级类，不能继承自其他类；
	 * 		 且本身具备默认构造函数或者无参构造函数
	 * 		 2、官方承认，这样的解析效率比自定义的解析要低
	 * @see com.yhty.dao.HouseDao#getByFileCode(java.lang.String)
	 */
//	public House getByFileCode(String fileCode) {
//		String sql = "select * from house where file_code=?";
//		Object[] o = new Object[] { fileCode };
//		return (House) getJdbcTemplate().queryForObject(sql, o,
//				BeanPropertyRowMapper.newInstance(House.class));
//	}
	
	/*
	 * ParameterizedBeanPropertyRowMapper和BeanPropertyRowMapper
	 * 的区别在于ParameterizedBeanPropertyRowMapper会根据参数类型
	 * 自动识别返回值的类型，当然，效率更低
	 */
	public House getByFileCode(String fileCode) {
		String sql = "select * from house where file_code=?";
		Object[] o = new Object[] { fileCode };
		return getJdbcTemplate().queryForObject(sql, o,
				ParameterizedBeanPropertyRowMapper.newInstance(House.class));
	}

	/**
	 * 更新房屋信息
	 */
	public void update(House h) {
		String sql = "update house set address="+h.getAddress()
		+",hos_floor="+h.getHos_floor()+",location="+h.getLocation()
		+",bump_code="+h.getBump_code()+",room_code="+h.getRoom_code()
		+",use_area="+h.getUse_area()+",build_area="+h.getBuild_area()
		+",common_area="+h.getCommon_area()+",status="+h.getStatus()
		+" where file_code="+h.getFile_code();
		jdbcTemplate.update(sql);
		
	}
}
